<?php
class Auth_model extends CI_Model {
	
	function Auth_model () {
		parent::__construct();
		$this->load->database();
	}
	
	function authenticate($username, $password) {
		$this->db->where('username', $username);
		$this->db->where('password', $password);
		$query = $this->db->get('accounts');
		
		if ($query->num_rows () > 0) {
			return TRUE;	
		}else {
			return 0;
		}
	}
	
	function getPermissionLevel($username, $module) {
		$this->db->where('username', $username);
		$query = $this->db->get('accounts_permissions');
		if($query->num_rows() > 0) {
			$row = $query->first_row();
			return $row->$module;
		}
	}
	
	function getPermissions($username) {
		$this->db->where('username', $username);
		$query = $this->db->get('accounts_permissions');
		if($query->num_rows() > 0) {
			$row = $query->first_row();
			$permissions = array(
				'overview'=>$row->overview ,
				'provider'=>$row->provider ,
				'category'=>$row->category ,
				'timepolicy'=>$row->timepolicy ,
				'pricepolicy'=>$row->pricepolicy ,
				'customer'=>$row->customer ,
				'visitor'=>$row->visitor ,
				'order'=>$row->order ,
				'cardtype'=>$row->cardtype ,
				'card'=>$row->card ,
				'news'=>$row->news ,
				'slideshow'=>$row->slideshow ,
				'adv'=>$row->adv ,
				'syspage'=>$row->syspage ,
				'account'=>$row->account 
			);
			return $permissions;
		}
	}
	
	function updatePermissions($username, $permissions) {
		$this->db->where('username', $username);
		return $this->db->update('accounts_permissions', $permissions);
	}
	
	function setPermissionAll($username, $level) {
		$permissions = array(
					'overview'=>$level ,
					'provider'=>$level ,
					'category'=>$level ,
					'timepolicy'=>$level ,
					'pricepolicy'=>$level ,
					'customer'=>$level ,
					'visitor'=>$level ,
					'order'=>$level ,
					'cardtype'=>$level ,
					'card'=>$level ,
					'news'=>$level ,
					'slideshow'=>$level ,
					'adv'=>$level ,
					'syspage'=>$level ,
					'account'=>$level
				);
		return $this->updatePermissions($username, $permissions);
	}
}